package cn.fuxue.hrms.web.admin_controller;

import cn.fuxue.hrms.common.dto.CvInfoShow;
import cn.fuxue.hrms.common.entity.CvInfo;
import cn.fuxue.hrms.common.utils.ResumeStructured;
import cn.fuxue.hrms.common.utils.Upload;
import cn.fuxue.hrms.service.CvService;
import cn.fuxue.hrms.service.impl.CvServiceImpl;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author:fuxue
 * description:后台 简历管理 接口+视图解析
 */
@Controller
@RequestMapping("/admin/cv")
public class CvController {
    @Autowired
    private CvServiceImpl cvService;

    /**
     * 查看所有简历信息
     *
     * @param pageNum
     * @param pageSize
     * @param likename
     * @param model
     * @return
     */
    @GetMapping("/find")
    public String find(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "likename", defaultValue = "") String likename,
            Model model
    ) {
        List<CvInfoShow> cvInfoShowList = cvService.findCvInfo(likename);
        model.addAttribute("cvInfoList", cvInfoShowList);
        return "admin/cv";
    }

    /**
     * 简历详情页面
     *
     * @param cvId    简历id
     * @param eduBack 学历
     * @param model
     * @return
     */

    @GetMapping("/find/{cv_id}")
    public String select(
            @PathVariable("cv_id") Integer cvId,
            @RequestParam("eduBack") Integer eduBack,
            Model model) {

//        学历加入域中
        model.addAttribute("eduBack", eduBack);
//        map接受 法1：
//        List<Map<String,Object>> cvInfoList=cvService.findCvInfoById(cvId);
//        非空判断后面+上
        CvInfo cvInfo = cvService.selectByPrimaryKey(cvId);
        model.addAttribute("cvInfo", cvInfo);
        return "admin/cvDetail";
    }

    /**
     * 导入简历 对接Ai
     *
     * @param file
     * @param userId
     * @return
     */
    @PostMapping("/add")
    public String add(@RequestParam("file") MultipartFile file,
                      @RequestParam("userId") Integer userId) throws Exception {
        CvInfo cvInfo = new CvInfo();
        String fileUrl = Upload.upload(file);
        ////        方式1：不断的set + 空指针判断
//        cvInfo.setFileUrl(fileUrl);
//        cvInfo.setUserId(userId);
//        方式2：重构过的
        Map<String, Object> map = new HashMap<>();
        map.put("fileUrl", fileUrl);
        map.put("userId", userId);
        cvInfo.populateFromJson(map);
        cvService.insert(cvInfo);
//        新增关联表
        String data=ResumeStructured.parser(Upload.path+fileUrl);
        Map<String,Object> res = JSON.parseObject(data,Map.class);
        res.put("cvId",cvInfo.getId());
        cvService.addOtherService(res);
        this.addOtherInfo(res);
        return "redirect:/admin/cv/find";
    }


    @PostMapping("/update/{id}")
    public String update(@PathVariable("id") Integer id,@RequestParam("file") MultipartFile file) throws Exception {
        CvInfo cvInfo = new CvInfo();
        String fileUrl = Upload.upload(file);
        ////        方式1：不断的set + 空指针判断
//        cvInfo.setFileUrl(fileUrl);
//        cvInfo.setUserId(userId);
//        方式2：重构过的
        Map<String, Object> map = new HashMap<>();
        map.put("fileUrl", fileUrl);
        map.put("userId", id);
        cvInfo.populateFromJson(map);
        cvService.updateByPrimaryKey(cvInfo);
//        新增关联表
        String data=ResumeStructured.parser(Upload.path+fileUrl);
        Map<String,Object> res = JSON.parseObject(data,Map.class);
        res.put("cvId",cvInfo.getId());
        cvService.addOtherService(res);
        this.addOtherInfo(res);
        return "redirect:/admin/cv/find"+id;
    }




    @GetMapping("delete")
    public String delete(
            @RequestParam("cv_id") Integer id
    ) {
        int res=cvService.deleteByPrimaryKey(id);
        return "redirect:/admin/cv/find";
    }

    public Integer addOtherInfo(Map<String, Object> map) {

        return null;
    }


}
